package com.ls.customer.service;

import com.ls.customer.entity.*;
import com.ls.customer.repository.*;
import com.ls.customer.utils.IdWorker;
import com.ls.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author 钰杰
 * @className CustomerService
 * @description VX:FengYJ996
 * @date 2020/1/28:56
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private PersonalRepository personalRepository;

    @Autowired
    private CustInterRepository custInterRepository;

    @Autowired
    private InterService interService;

    @Autowired
    private CustInterLogRepository custInterLogRepository;

    //获取用户详细信息
    public Map<String, Object> getCustomerByAppid(String appid) {
        Map<String, Object> customerInfo = new HashMap<>();
        //获取appid
        Customer customerByAppid = customerRepository.findCustomerByAppid(appid);
        if (StringUtils.isEmpty(customerByAppid)) {

            return null;
        }

        Integer userType = customerByAppid.getUserType();
        customerInfo.put("basicInfo", customerByAppid);//基本信息

        if (1 == userType) {          //企业信息
            VerifiedCompany verifiedCompanyByAppid = companyRepository.findVerifiedCompanyByAppid(appid);
            customerInfo.put("detailInfo", verifiedCompanyByAppid);
        } else if (2 == userType) {   //个人信息
            VerifiedPersonal verifiedPersonalByAppid = personalRepository.findVerifiedPersonalByAppid(appid);
            customerInfo.put("detailInfo", verifiedPersonalByAppid);
        }

        return customerInfo;
    }

    //获取当前appid下所有接口
    public List<CustInter> getInterlist(String appid) {
        return custInterRepository.findAllByCustId(appid);
    }

    //支付订单
    @Transactional
    public ResultCode payForOrder(Integer interoid) {
        InterOrder interOrder = interService.getinterOrder(interoid);
        Customer customerByAppid = customerRepository.findCustomerByAppid(interOrder.getAppid());
        if (customerByAppid.getBalance() < interOrder.getAmount()) {//余额不足本次操作
            return ResultCode.NOT_SUFFICIENT_FUNDS;
        }
        double resultBalance = customerByAppid.getBalance() - interOrder.getAmount();
        if (resultBalance < 0) {   //余额不足扣款
            return ResultCode.NOT_SUFFICIENT_FUNDS;
        }
        //更改余额
        customerRepository.updBalanceByAppid(interOrder.getAppid(), resultBalance);
        interService.updInterOrderStatus("1", interoid);
        return ResultCode.OK;
    }

    //生成用户接口中间表
    @Transactional
    public void addCustInter(InterOrder newInterOrder) {
        IdWorker idWorker = new IdWorker();
        String appid = newInterOrder.getAppid();
        String count = newInterOrder.getCount();
        Integer interid = newInterOrder.getInterid();
        Integer payType = newInterOrder.getPayType();

        //生成用户接口秘钥
        Calendar instance = Calendar.getInstance();
        Date sdate = instance.getTime();//起始日期
        Date edate;//截止日期
        //初始化基本信息
        long l = idWorker.nextId();
        long time = sdate.getTime();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(l);
        stringBuffer.append(time);

        CustInter newCustInter = new CustInter();


        newCustInter.setDstatus(1);
        newCustInter.setCustId(appid);

        newCustInter.setAppkey(stringBuffer.toString());
        newCustInter.setInterId(interid);
        newCustInter.setSdate(sdate);
        newCustInter.setStatus(1);

        switch (payType) {
            case 1://包月
                instance.add(Calendar.MONTH, Integer.parseInt(count));
                edate = instance.getTime();
                newCustInter.setPriceType("1");
                newCustInter.setEdate(edate);
                break;
            case 2://包年
                instance.add(Calendar.YEAR, Integer.parseInt(count));
                edate = instance.getTime();
                newCustInter.setPriceType("2");
                newCustInter.setEdate(edate);
                break;
            case 3://按次购买
                newCustInter.setPriceType("3");
                newCustInter.setSumCount(Integer.parseInt(count));
                break;
        }

        CustInter oldCustInter = this.getCustInter(newInterOrder.getAppid(), newInterOrder.getInterid());
        if (!StringUtils.isEmpty(oldCustInter)) {//不为空则修改
            newCustInter.setId(oldCustInter.getId());
        }

        //购买记录
        CustInter custInterForLog = custInterRepository.save(newCustInter);

        CustInterLog custInterLog = new CustInterLog();
        custInterLog.setAppid(custInterForLog.getCustId());
        custInterLog.setCustInterLog(custInterForLog.toString());
        custInterLogRepository.save(custInterLog);
    }

    //查询用户购买的指定接口
    public CustInter getCustInter(String appid, Integer interid) {
        return custInterRepository.findByCustIdAndInterId(appid, interid);
    }


}